package com.spn.blog.api.handler;

//拦截器,写完告诉springMVC，拦截器才生效
//这只是拦截方式，但是还需要设置拦截路径，在WebMVCConfigure中设置
//@Component 该类由security代替
/*@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @DubboReference
    private LoginService loginService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //在执行Controller方法（handler）之前，执行以下内容
        *//**
         * 1. 需要判断请求的接口路径是否为HandlerMethod（controller方法）
         * 2. 判断 token 是否为空，如果为空==未登录
         * 3. 如果 token 不为空，进行登录验证，loginService checkToken
         * 4. 如果认证成功，放行即可
         *//*
        if (!(handler instanceof HandlerMethod)) {
            //不是controller中的方法就直接放行，不拦截静态资源访问，例如RequestResourceHandler
            return true;
        }

        String token = request.getHeader("Authorization");
        String token333 = request.getHeader("token333");
        request.getHeaderNames();
        Enumeration<String> headerNames = request.getHeaderNames();

        String requestURI = request.getRequestURI();

        log.info("\n");
        log.info("=========================Request start======================");
        log.info("Request uri:{}", requestURI);
        log.info("Request method:{}", request.getMethod());
        log.info("token:{}", token);
        log.info("=========================Request end======================\n");
        //判断 token 是否为空
        if (StringUtils.isBlank(token333)) {
            if (StringUtils.isBlank(token)) {
                Result result = Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
                response.setContentType("application/json;charset=utf-8");
                response.getWriter().print(JSON.toJSONString(result));
                return false;
            }
        }

        SysUser sysUser = loginService.checkToken(token);
        //判断 token 是否合法
        if (StringUtils.isNotBlank(token333)){
            sysUser = loginService.checkToken(token333);
        }

        if (sysUser == null) {
            Result result = Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(JSON.toJSONString(result));
            return false;
        }
        //登录验证成功，放行
        //我希望在controller直接获取用户信息，怎么获取？使用ThreadLocal
        UserThreadLocal.put(sysUser);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //如果不删除 ThreadLocal 中用完的信息，会有内存泄漏的风险。
        UserThreadLocal.remove();
    }
}*/
